<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="closeFn">
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"

          @focus="isshow = true"
        />
        <!-- @blur="isshow =false" -->
        <!-- 默认全部显示 -->
        <!--  @node-click是element组件里面tree的事件，	节点被点击时的回调 -->
        <!-- 共三个参数，依次为：传递给 data 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 -->
        <el-tree
          v-show="isshow"
          :data="departmentList"
          :props="{ label: 'name' }"
          default-expand-all
          @node-click="selectDepartment"
        />
        <!-- :props="{label:'name'} 选择渲染的内容，根据数组的属性名来渲染，如果你是label:'code'，那他渲染出来的就是这个数组里面的code内容-->
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-row>
        <el-button type="paimary" size="small" @click="confirmFn">
          确定
        </el-button>
        <el-button size="small" @click="closeFn"> 取消 </el-button>
      </el-row>
    </span>
  </el-dialog>
</template>

<script>
// 引入这里的文件，作用于新增的时候的部门的下拉列表
import employees from '@/api/constant/employees'
import { getDepartment, deleteEmployees } from '@/api/department'
import { listtotree } from '../../../utils/index'
import { addEmployees } from '../../../api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
      //   因为这是自组件，不能直接修改数据需要子传父，然后element自带的你要给他绑定事件你要查看文档有没有
    }
  },
  data() {
    return {
      isshow: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 1, max: 10, message: '用户名长度1到10为', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空！', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
          // /d是0-9，第一位是1，第二位是3-9，第三到第十一位为0-9
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空！', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空！', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门名称不能为空！', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空！', trigger: 'blur' }
        ]
      },
      hireType: employees.hireType,
      departmentList: []
    }
  },
  async created() {
    const res = await getDepartment()
    console.log('666666res: ', res)
    // 把接口调用出来的数据转成树形接口
    this.departmentList = listtotree(res.depts, '')
  },
  methods: {
    closeFn() {
      // 重置表单
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清空验证
      this.$refs.form.resetFields()
      this.isshow = false
      // 父传子关闭弹窗
      this.$emit('closeEvent')
    },
    // 确定按钮
    async confirmFn() {
      await this.$refs.form.validate()
      console.log('验证通过')
      await addEmployees(this.formData)
      this.closeFn()
      this.$emit('addEvent', this.formData)
    },
    selectDepartment(data) {
      console.log('data: ', data)
      this.formData.departmentName = data.name
      this.isshow = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  position: relative;
  .el-tree {
    height: 150px;
    width: 50%;
    position: absolute;
    z-index: 1;
    overflow-y: auto;
  }
}
</style>

<style>
::-webkit-scrollbar-track-piece {
  border-radius: 0
}
::-webkit-scrollbar {
  width: 5px;
  height: 10px
}
::-webkit-scrollbar-thumb {
  height: 50px;
  background-color: #b8b8b8;
  border-radius: 6px;
  outline-offset: -2px;
  filter: alpha(opacity = 50);
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5
}
::-webkit-scrollbar-thumb:hover {
  height: 50px;
  background-color: #878987;
  border-radius: 6px
}
</style>
